package com.dyc.caradvertisting.ui.fmt.bind.bs;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dyc.caradvertisting.R;
import com.dyc.caradvertisting.bean.req.BindBean;
import com.dyc.caradvertisting.comm.GsonUtils;
import com.dyc.caradvertisting.event.ParamBrotherEvent;
import com.dyc.caradvertisting.event.StartBrotherEvent;
import com.dyc.caradvertisting.ui.fmt.bind.BindSerContract;
import com.dyc.caradvertisting.ui.fmt.bind.BindSerModel;
import com.dyc.caradvertisting.ui.fmt.bind.BindSetPresenter;
import com.njh.tools.base.BaseFragment;
import com.njh.tools.common.ToastUtils;
import com.njh.tools.common.userInfo.SPUserInfo;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 绑定设备信息
 */
public class BindSetFmt extends BaseFragment<BindSetPresenter, BindSerModel> implements BindSerContract.View {


    @BindView(R.id.btn_bind_set)
    Button btnBindSet;
    @BindView(R.id.tv_car_number)
    TextView tvCarNumber;
    @BindView(R.id.ly_select_number)
    LinearLayout lySelectNumber;
    @BindView(R.id.edt_number)
    EditText edtNumber;
    @BindView(R.id.edt_ns)
    EditText edtNs;
    @BindView(R.id.rt_ewm)
    RelativeLayout rtEwm;

    public BindSetFmt() {
        // Required empty public constructor
    }

    public static BindSetFmt newInstance() {
        BindSetFmt fragment = new BindSetFmt();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return getView(inflater, container);
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_bind_set_fmt;
    }

    @Override
    public void initView(View baseView) {
        EventBus.getDefault().register(this);
        initToolbarNav(R.string.title_bind_set);
        ButterKnife.bind(this, baseView);

    }

    @Override
    public boolean isSwipeBack() {
        return true;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel, this._mActivity);
    }

    @Override
    public void finish() {
        this._mActivity.onBackPressed();
    }

    /**
     * start other BrotherFragment
     */
    @Subscribe
    public void changBrother(ParamBrotherEvent event) {
        if (event.getType() == 1) {
            tvCarNumber.setText(event.getMesg());
        }
    }

    @OnClick({R.id.ly_select_number, R.id.btn_bind_set,R.id.rt_ewm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ly_select_number:
                // 这里我使用EventBus通知父MainFragment处理跳转(接耦),
                EventBus.getDefault().post(new StartBrotherEvent(SelectCarNmberFmt.newInstance()));
                break;
            case R.id.btn_bind_set:
                String number = edtNumber.getText().toString();
                if (TextUtils.isEmpty(number)) {
                    ToastUtils.showLong(this.getContext(), "请输入车牌号");
                    return;
                }
                String ns = edtNs.getText().toString();
                if (TextUtils.isEmpty(ns)) {
                    ToastUtils.showLong(this.getContext(), "请扫描设备上的二维码");
                    return;
                }
                BindBean bindBean = new BindBean(ns, number, SPUserInfo.getUid(this.getContext()), tvCarNumber.getText().toString());
                mPresenter.reqBindSet(GsonUtils.getmGson().toJson(bindBean));
                break;
            case R.id.rt_ewm://二维码
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
